package cn.qhuan.modules.service.impl;

import cn.qhuan.modules.dtos.ArtistAbbDto;
import cn.qhuan.modules.dtos.ArtistAuthDto;
import cn.qhuan.modules.dtos.ArtistDto;
import cn.qhuan.modules.entity.Artist;
import cn.qhuan.modules.mapper.ArtistMapper;
import cn.qhuan.modules.mapper.WorksMapper;
import cn.qhuan.modules.service.IArtistService;
import cn.qhuan.modules.constos.PageResponseResult;
import cn.qhuan.modules.constos.StatusConstos;
import cn.qhuan.modules.entity.Dynasty;
import cn.qhuan.modules.mapper.DynastyMapper;
import cn.qhuan.modules.entity.Works;
import cn.qhuan.modules.mapper.WorksMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 艺术家表
 * @Author: 司翔宇
 * @Date:   2020-12-28
 * @Version: V1.0
 */
@Service
public class ArtistServiceImpl extends ServiceImpl<ArtistMapper, Artist> implements IArtistService {

    @Autowired
    private DynastyMapper dynastyMapper;
    @Autowired
    private ArtistMapper artistMapper;

    @Autowired
    private WorksMapper worksMapper;

    @Override
    public Result<?> findByCondition(ArtistDto dto) {

        //1.参数检查
        if (dto == null){
            return Result.error("缺少参数");
        }

        //分页参数检查
        dto.checkParam();
        //2.分页条件查询
        IPage pageParam =new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Artist> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        //按照关键字模糊查询
        if (StringUtils.isNotBlank(dto.getKeyword())){
            lambdaQueryWrapper.like(Artist::getName,dto.getKeyword());
        }
        //按照朝代精准查询
        if (StringUtils.isNotBlank(dto.getDynastyId())){
            lambdaQueryWrapper.eq(Artist::getBirthdayDate,dynastyMapper.selectById(dto.getDynastyId()).getName());
        }
        if (StringUtils.isNotBlank(dto.getStatus())){
            lambdaQueryWrapper.eq(Artist::getStatus,dto.getStatus());
        }
        //3.封装结果返回
        IPage pageList = page(pageParam, lambdaQueryWrapper);
        PageResponseResult responseResult =new PageResponseResult(dto.getPage(),dto.getSize(), (int) pageList.getTotal());
        responseResult.setResult(pageList.getRecords());
        return responseResult;
    }

    @Override
    public Result<?> updateStatus(ArtistAuthDto dto, Integer type) {
        //1.检查参数
        if (dto ==null || dto.getId() == null ){
            return Result.error("缺少参数");
        }
        //2.查询作品信息
        Artist artist = artistMapper.selectById(dto.getId());
        if (artist == null){
            return Result.error("艺术家不存在");
        }
        //3.审核失败
        if (type.equals(StatusConstos.STATUS_FAIL)){
            updateArtist(artist,StatusConstos.STATUS_FAIL);
        }else if (type.equals(StatusConstos.STATUS_PASS)){
            updateArtist(artist,StatusConstos.STATUS_PASS);
        }
        return Result.OK("修改成功");
    }



    @Override
    public Result<?> findByAbbreviation(ArtistAbbDto dto) {
        //1.检查参数
        if (dto == null ){
            return Result.error("缺少参数");

        }
        //分页参数检查
        dto.checkParam();
        //2.分页条件查询
        IPage pageParam =new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Artist> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        //根据字母缩写模糊查询
        if (StringUtils.isNotBlank(dto.getAbbreviation())){
            lambdaQueryWrapper.like(Artist::getAbbreviation,dto.getAbbreviation());
        }
        //3.封装结果返回
        IPage pageList = page(pageParam, lambdaQueryWrapper);
        PageResponseResult responseResult =new PageResponseResult(dto.getPage(),dto.getSize(), (int) pageList.getTotal());
        responseResult.setResult(pageList.getRecords());
        return responseResult;
    }

    @Override
    public Result<?> addArtistToDynasty(String uuid, String artistId, String dynastyId) {
        //参数判断
        if (uuid == null || artistId == null || dynastyId == null ){
            return Result.error("缺少参数");
        }
        if (artistMapper.selectCount(artistId) == 0){
            Artist artist = artistMapper.selectById(artistId);
            Dynasty dynasty = dynastyMapper.selectById(dynastyId);
            artist.setBirthdayDate(dynasty.getName());
            artistMapper.updateById(artist);
            artistMapper.addArtistToDynasty(uuid,artistId,dynastyId);
            return Result.OK("朝代设置成功");
        }else {
            Artist artist = artistMapper.selectById(artistId);
            Dynasty dynasty = dynastyMapper.selectById(dynastyId);
            artist.setBirthdayDate(dynasty.getName());
            artistMapper.updateById(artist);
            artistMapper.deleteAssociation(artistId);
            artistMapper.addArtistToDynasty(uuid, artistId, dynastyId);
            return Result.OK("朝代修改成功");
        }

    }

    @Override
    public Result<?> queryWorksByArtistId(ArtistDto dto) {
        if (dto == null ){
            return Result.error("缺少参数");
        }
        //设置分页条件
        dto.setPage((dto.getPage()-1)*dto.getSize());
        List<Works> worksList = artistMapper.findWorksByArtistId(dto);
        //统计数量
        Integer count = artistMapper.queryWorksCountByArtistId(dto.getArtistId());
        //封装结果进行返回
        PageResponseResult pageResponseResult =new PageResponseResult(dto.getPage(),dto.getSize(),count);
        pageResponseResult.setResult(worksList);
        return pageResponseResult;

    }


    @Override
    public Result<?> queryWorksByEhxhiOnArtist(String artistId,String exhiId) {
        //1.检查参数
        if ( exhiId == null ){
            return Result.error("缺少参数");
        }
        //2.根据传过来的展厅id查询相关作品id
        List<String> worksIdsList = worksMapper.findWorksIdsByEhxhiId(exhiId);
        List<Works> qhList =new ArrayList<>();
        Works qhWorks= null ;
        for (String worksId : worksIdsList) {
            qhWorks = worksMapper.selectById(worksId);
            if (qhWorks.getArtistId().equals(artistId)){
                qhList.add(qhWorks);
            }
        }
        return Result.OK(qhList);
    }

    @Override
    public Result<?> queryWorksCountOnArtist(String artistId) {
        if (artistId == null ){
            return Result.error("缺少参数");
        }
        Integer count = artistMapper.queryWorksCountByArtistId(artistId);
        return Result.OK(count);
    }

    @Override
    public Result<?> queryDocumentCountOnArtist(String artistId) {
        if (artistId == null ){
            return Result.error("缺少参数");
        }
        Integer count = artistMapper.queryDocumentCountByArtistId(artistId);
        return Result.OK(count);
    }

    /**
     * 修改作品信息
     * @param artist
     * @param status
     *
     */
    private void updateArtist (Artist artist,Integer status){
        artist.setStatus(status.toString());
        artistMapper.updateById(artist);
    }
}
